#include "threading.h"

using namespace std;

void ThreadedServer::StartServer() {
	if(!Bind()) return;
	this->Server::StartServer();
	cout<<"Server started at: "<<addr->ToString()<<endl;
	while(true) {
		sockaddr a;
		int t=sizeof(sockaddr);
		unsigned th;
		THREAD_DATA *td=new THREAD_DATA();
		td->sock=accept(sock,&a,&t);
		td->addr=new Address(&a);
		td->server=this;
		HANDLE h=(HANDLE)_beginthreadex(NULL,0,HandleEx,td,0,&th);
		CloseHandle(h);
	}
}
unsigned __stdcall ThreadedServer::HandleEx(LPVOID lpParam) {
	THREAD_DATA *t=(THREAD_DATA *)lpParam;
	((ThreadedServer *)t->server)->handle(t);
}
ThreadedServer::ThreadedServer():Server(){}
void ThreadedServer::SetThreads(int t) {
	threads=t;
}
void ThreadedServer::SetHandle(FUNC func) {
	handle=func;
}
